package modeller.queueingnetworksmodel.structure;

import modeller.databasedesignmodel.relation.Relation;
import modeller.databasedesignmodel.servicedemand.IServiceDemand;
import modeller.databasedesignmodel.transaction.Transaction;
import modeller.queueingnetworksmodel.probabilities.TransactionRoutingMatrix;
import org.jdom2.Element;

import java.util.HashMap;
import java.util.HashSet;

/**
 * Created by:  Jason Ye
 * Date:        30/05/2012
 * Time:        20:50
 */
public class Node implements IQueueingNetworkModelXMLNode {

    private Relation relation;

    private QueueElement queueElement;
    private PSServerElement psServerElement;
    private RouterElement routerElement;
    private HashMap<Transaction, IServiceDemand> serviceDemands;

    public Node(Relation relation, TransactionRoutingMatrix qnInstantaneousTransitionMatrix, HashSet<Transaction> transactions, HashMap<Transaction, IServiceDemand> serviceDemands) {

        this.relation=relation;
        this.serviceDemands = serviceDemands;

        // init child elems
        this.queueElement=new QueueElement(transactions,relation);

        this.psServerElement=new PSServerElement(transactions,relation,serviceDemands);
        this.routerElement=new RouterElement(qnInstantaneousTransitionMatrix,relation);

    }

    /**
     * special constructor for router node
     * @param qnInstantaneousTransitionMatrix
     * @param transactions
     */
    public Node(TransactionRoutingMatrix qnInstantaneousTransitionMatrix, HashSet<Transaction> transactions){

        this.queueElement = new QueueElement(transactions,relation);
        Relation router = new Relation(22,2,2,2);
        router.setRelationName("Router");
        this.psServerElement = new PSServerElement(transactions,router,new HashMap<Transaction, IServiceDemand>());

    }

    @Override
    public Element toXMLRepresentation() {

        Element result = new Element("node");
        result.setAttribute("name",relation.getRelationName());

        result.addContent(queueElement.toXMLRepresentation());
        result.addContent(psServerElement.toXMLRepresentation());
        result.addContent(routerElement.toXMLRepresentation());

        return result;
    }
}
